'use strict';

App.controller('bidController', ['$rootScope', '$scope', '$state', 'Page','$interval', function($rootScope, $scope, $state, Page, $interval) {

		//时间显示
		$scope.showCountDown = function(bid) { 
		   	var limitTime = bid.limitTime;
		   	var endDate = new Date(limitTime); 
		   	var now = new Date(); 
	        var leftTime=endDate.getTime()-now.getTime(); 
	        var leftsecond = parseInt(leftTime/1000); // 剩下秒数
	        var day=Math.floor(leftsecond/(60*60*24)); 
	        var hour=Math.floor((leftsecond-day*24*60*60)/3600); 
	        var minute=Math.floor((leftsecond-day*24*60*60-hour*3600)/60); 
	        var second=Math.floor(leftsecond-day*24*60*60-hour*3600-minute*60); 
	        if(hour<10){
	       	 hour = '0'+hour;
	        }
	        if(minute<10){
	       	 minute = '0'+minute;
	        }
	        if(second<10){
	       	 second = '0'+second;
	        }
	        bid.limitTimeDay = day;
	        bid.limitTimeHour = hour;
	        bid.limitTimeMinute = minute;
	        bid.limitTimeSecond = second;
		}
	
	
		$scope.bidId =	$state.params.bidId;
		if($rootScope.user!=undefined){
			$scope.customerId =	$rootScope.user.sourceId;
		}else { // 没有登录
			$scope.customerId = "";
		}
		
		/*if($scope.customerId == '' || $scope.customerId == null){
			$state.go('login', null);
			Page.notice("获取登录信息失败!");
			return;
		}*/
		
		
		$scope.initData = function(){
			var param = {};
			param.bidId = $scope.bidId;
			param.customerId = $scope.customerId;
			
			Page.ajaxPost('anon/www/bid/findBidByBidId.do',param,function(resp){
				 $scope.bid = resp.data.bid;
				 $scope.showCountDown($scope.bid);
				 
				 	// 项目出价流水
					Page.ajaxPost('anon/www/bid/findBidRecordInfo.do',param,function(resp){
						 $scope.topRecord = resp.data.topRecord;// 当前最高价
						 $scope.bidRecordArray = resp.data.bidRecordArray;// 出价记录
						 if($scope.bidRecordArray.length>0){
							if($scope.myPrice != undefined && $scope.myPrice > $scope.topRecord.bidPrice){
								 $scope.payPrice = $scope.myPrice;
								 $scope.basePrice = $scope.topRecord.bidPrice;// 客户出价最低价  (当前最高价)
							 } else {// 自己没有出价
								 $scope.payPrice = $scope.topRecord.bidPrice;// 客户出价最低价  (当前最高价)
								 $scope.basePrice = $scope.payPrice; 
							 }
						 }else {
							 if($scope.myPrice != undefined && $scope.myPrice > $scope.bid.basePrice){
								 $scope.payPrice = $scope.myPrice;
								 $scope.basePrice = $scope.bid.basePrice;// 客户出价最低价  (起拍价)
							 } else {// 自己没有出价
								 $scope.payPrice = $scope.bid.basePrice; // 客户出价最低价 (起拍价)
								 $scope.basePrice = $scope.payPrice;
							 }
						 }
					});
					
					// 项目关联媒体资源信息
					Page.ajaxPost('anon/www/bid/getBidResourceGroupByTypes.do', param, function (resp) {
						$scope.mediaTypes = resp.data.array;
					});
					
					// 查看客户是否交保证金
					Page.ajaxPost('anon/www/bid/getCustomerDeposit.do', param, function (resp) {
						$scope.dataArray = resp.data.dataArray;
						if(resp.data.dataArray.length>0){
							$scope.flag = true;
						}else{
							$scope.flag = false;
						} 
					});

					// 查看客户是否存在支付中的订单（有订单状态为支付中的，按钮显示“支付中”）
					Page.ajaxPost('anon/www/bid/getCustomerDepositOrder.do', param, function (resp) {
						$scope.hasPayingOrder = resp.data.hasPayingOrder;
					});
			});
			
			//$interval.cancel($scope.timer); // 终止定时器
		}
		
		$scope.initData();// 初始化页面数据
	    
	    $scope.bidForm = {
	    		payDeposit : function() {
	    			if($scope.customerId==''){ // 没有登录
	    				$state.go('login', null);
	    				return;
	    			}
	    			// 交保证金  暂时简单处理，直接插入数据，后期调支付接口...
	    			Page.confirm("保证金额为：¥ "+$scope.bid.deposit+".00 ,您确定支付吗?", function () {
		    			/*var param = {};
		    			param.customerId = $scope.customerId;
		    			param.bidId = $scope.bid.bidId;
		    			param.deposit = $scope.bid.deposit;
		    			Page.ajaxPost('www/bid/payDeposit.do', param, function (resp) {
		    				//$scope.initData();// 重新加载页面数据
		    				//Page.notice("您已支付该项目 "+param.deposit+".00  的保证金!");
		    			});*/
	    				window.open("payOrder/payDeposit.do?bidId="+$scope.bid.bidId+"&customerId="+$scope.customerId);          
	    			});
	    		},
	    		
	    		addPrice : function() {// 加价
	    			var increasePrice = $scope.bid.increasePrice;
	    			$scope.payPrice = parseInt($scope.payPrice)+parseInt(increasePrice);
	    			$scope.myPrice = $scope.payPrice;// 我的出价
	    		},
	    		minusPrice : function() {// 减价
	    			var increasePrice = $scope.bid.increasePrice;
	    			if(parseInt($scope.payPrice)>parseInt($scope.basePrice)){
	    				$scope.payPrice = parseInt($scope.payPrice)-parseInt(increasePrice);	
	    				$scope.myPrice = $scope.payPrice;// 我的出价
	    			}else {
	    				Page.notice("出价必须高于当前价!");
	    			}
	    		},
	    		offerPrice : function() {// 出价 (再次与订单价格进行比对)
	    			var param = {};
	    			param.bidId = $scope.bidId;
	    			param.customerId = $scope.customerId;
	    			Page.ajaxPost('anon/www/bid/findBidByBidId.do',param,function(resp){
	    				$scope.bid_o = resp.data.bid;
		    			
	    				Page.ajaxPost('anon/www/bid/findBidRecordInfo.do',param,function(resp){// 查询出价记录详情
		    				 $scope.topRecord_o = resp.data.topRecord;// 当前最高价
							 $scope.bidRecordArray_o = resp.data.bidRecordArray;// 出价记录
							 if($scope.bidRecordArray_o.length>0){
								 $scope.basePrice_o = $scope.topRecord_o.bidPrice;// 客户出价最低价 (当前最高价)
							 }else {// 没有出价记录
								 $scope.basePrice_o = $scope.bid_o.basePrice; // 客户出价最低价(起拍价)
							 }
							 if($scope.payPrice>$scope.basePrice_o){
				    				Page.confirm("您给出的价钱为：¥ "+$scope.payPrice+".00 ,您确定出价吗?", function () {
					    				var param = {};
						    			param.payPrice = $scope.payPrice;
						    			param.customerId = $scope.customerId;
						    			param.bidId = $scope.bidId;
						    			Page.ajaxPost('www/bid/offerPrice.do', param, function (resp) {
						    				Page.notice("出价成功! 祝您好运!");
						    				$scope.initData();// 重新加载页面数据
						    			});
				    				});
				    			}else {
				    				Page.notice("出价无效! 出价必须高于当前价!");
				    			}
		    			});
	    			});
	    		},
	    		 callBack : function() {
			    	 window.history.back();
			    }
	    }

	    $rootScope.timer = $interval(function(){$scope.showCountDown($scope.bid);}, 1000);// 时间显示 每秒钟执行一次
	    
	    $rootScope.timerX = $interval(function(){$scope.initData();}, 5000);// 页面五秒钟刷新一次
	    
}]);